<?
/**
*  工单管理
*  2013/7/14 by zh
*/
include("../webkit/include/simple_db.php");

require("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];

if($action == "del") {
	$db->doDelete('jx_order', 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	if(intval($_GET['client_id']) > 0) {
		$condition = "client_id=".$_GET['client_id'];
	} else {
		$condition = "user_name='".$_SESSION['uid']."'";
		if($_SESSION['user_flag'] == 'admin' || $_SESSION['user_flag'] == 'callback' || $_SESSION['user_flag'] == 'check') {
			$condition = "1=1";   // 管理员可以查看所有工单
		}
		if(isset($_GET['client_name'])) {
			$condition .= " and client_name like '%".$_GET['client_name']."%'";
		}
		if(isset($_GET['order_status'])) {
			$condition .= " and order_status = ".$_GET['order_status'];
		}
		if(isset($_GET['telephone'])) {
			$n = $_GET['telephone'];
			$condition .= " and (mobilephone='$n' or telephone1='$n' or telephone2='$n' or telephone3='$n')";
		}
		if(isset($_GET['user_name'])) {
			$condition .= " and user_name='".$_GET['user_name']."'";
		}
		if(isset($_GET['agent_name'])) {
			$condition .= " and agent_name='".$_GET['agent_name']."'";
		}
		if(isset($_GET['callback_result'])) {
			$condition .= " and callback_result=".$_GET['callback_result'];
		}
		if(isset($_GET['time_range'])) {
			$arr = split("_", $_GET['time_range']);
			$condition .= " and (a.call_time between '".$arr[0]."' and '".$arr[1]."')";
		}
	}
	$sql = "select a.id,a.order_no, b.client_name,b.mobilephone,a.agent_name,a.call_time,a.user_name, a.expire_time,
	a.user_called, a.hasten_count, a.order_status, a.callback_result,a.check_result, '' as op_link, a.accept_time  
	from jx_order a Left Join jx_client b ON a.client_id=b.id where $condition order by id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("client_name"=>"time_color_mapper", "order_no"=>"time_color_mapper", "mobilephone"=>"time_color_mapper", "user_name"=>"red_color_mapper","op_link"=>"op_link_mapper"));

} else if($action == "get") {
	$sql = "select * from jx_order where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {
	unset($_POST['id']);
	$sql = "select count(0) from jx_order where order_no like '".date("Ymd")."%'";
	$cnt = $db->getOneValue($sql);
	$cnt++;
	$len = strlen($cnt."");
	for($i=0; $i<5-$len; $i++) {
		$cnt = '0'. $cnt;
	}
	$do_dispatch = false;
	if($_POST['user_name'] != '') { // 新增指定处理人，视为直接派单
		$_POST['order_status'] = "2";
		$_POST['dispatch_time'] = date("Y-m-d H:i:s");
		$do_dispatch = true;
	}
	$_POST['order_no'] = date("Ymd").$cnt;
	$_POST['call_time'] = date("Y-m-d H:i:s"); // 受理时间
	$_POST['expire_time'] = date("Y-m-d H:i:s", strtotime("+5 Hours"));  // 受理时间后5小时为回单时限
	$db->doInsert('jx_order', $_POST);
	write_history(&$db, "新增工单", $_POST['order_no']);
	if($do_dispatch) {
		write_history(&$db, "直接派单给(".$_POST['user_name'].")", $_POST['order_no']);
	}
	echo 'ok';

} else if($action == 'dispatch_order') {
	$sql = "update jx_order set user_name='".$_POST['user_name']."',dispatch_time=now(),order_status=2 where order_no = '".$_POST['order_no']."'";
	$db->query($sql);
	write_history(&$db, "确认派单给(".$_POST['user_name'].")", $_POST['order_no']);
	echo "ok";

} else if($action == 'accept_order') {
	$sql = "update jx_order set accept_text='".str_replace("'", "", $_POST['accept_text'])."', accept_time=now(),
	order_status=".$_POST['order_status']." where order_no = '".$_POST['order_no']."'";
	$db->query($sql);
	write_history(&$db, "回单(".$_POST['accept_text'].")", $_POST['order_no']);
	echo "ok";

} else if($action == 'hasten_order') {
	$sql = "update jx_order set hasten_count=hasten_count+1 where order_no = '".$_POST['order_no']."'";
	$db->query($sql);
	write_history(&$db, "催单", $_POST['order_no']);
	echo "ok";

} else if($action == 'change_appt') {
	$sql = "update jx_order set appt_time='".$_POST['appt_time']."', order_status=4 where order_no = '".$_POST['order_no']."'";
	$db->query($sql);
	write_history(&$db, "改约", $_POST['order_no']);
	echo "ok";

} else if($action == 'callback_order') {
	$sql = "update jx_order set callback_text='".str_replace("'", "", $_POST['callback_text'])."', callback_user='".$_SESSION['uid']."', callback_time=now(),
	callback_result=".$_POST['callback_result']." where order_no = '".$_POST['order_no']."'";
	$db->query($sql);
	write_history(&$db, "完成回访(".$_POST['callback_text'].")", $_POST['order_no']);
	echo "ok";

} else if($action == 'check_order') {
	$sql = "update jx_order set check_text='".str_replace("'", "", $_POST['check_text'])."', check_user='".$_SESSION['uid']."', check_time=now(),
	check_result=".$_POST['check_result']." where order_no = '".$_POST['order_no']."'";
	$db->query($sql);
	write_history(&$db, "完成质检(".$_POST['check_text'].")", $_POST['order_no']);
	echo "ok";

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate('jx_order', $_POST, 'id='.$id);
	write_history(&$db, "修改工单", $_POST['order_no']);
	echo 'ok';

} else if($action == "confirm_user_called") {
	$tel = $_GET['telephone'];
	$sql = "select order_no from jx_order where client_id in (select id from jx_client where mobilephone='$tel' or telephone1='$tel') 
	and call_time > '". date("Y-m-d H:i:s", strtotime("-30 minutes")) ."' order by id desc";
	//echo $sql;
	$order_no = $db->getOneValue($sql);  // 确认已有首次外勤联系 ( 受理30分钟之内有效 )
	$sql = "update jx_order set user_called=1 where order_no='$order_no'"; 
	//echo $sql;
	$db->query($sql);
	//write_history(&$db, "外勤联系", $order_no);
	echo "ok";

} else if($action == "get_history") {
	$no = $_GET['order_no'];
	$sql = "select * from jx_history where order_no='$no'";
	$db->query($sql);
	$arr = array();
	while($db->nextRecord()) {
		$rec = $db->getCurrentRecord();
		foreach($rec as $k=>$v) {
			if(is_int($k)) continue;
			$row[$k] = $v;
		}
		$arr[] = array_to_json($row);
	}
	echo "[".join(",", $arr)."]";
}



function op_link_mapper($v, $row) {
	return "<a href=# onclick=\"show_contact_record(\'".$row['mobilephone']."\', \'".$row['call_time']."\', \'".$row['accept_time']."\')\">查看录音</a>";
}

/*
function user_called_mapper($v, $row) {
	if($v == 0) {
		return "<font color=gray>未回</font>";
	}
	return "<font color=black>已回</font>"; 
}
*/

function time_color_mapper($v, $row) {
	$t = time() - strtotime($row['call_time']);
	$color = "black";
	if( $row['order_status'] == 2) { // 已派单待处理的记录，要提醒颜色
		if( $t > 5 * 3600) {
			$color = "red";
		} else if( $t > 4 * 3600) {
			$color = "#ff6600";
		}
	}
	return "<font color=\"$color\">$v</font>";
}
?>